package com.sskj.mine.ui.activity;


import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.PwdChangeActivityPresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.MD5Util;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.router.provider.LogoutProvider;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.MINE_PWD_CHANGE)//修改登录密码
public class PwdChangeActivity extends BaseActivity<PwdChangeActivityPresenter> {
    @BindView(R2.id.etPwdOrigin)
    EditText etPwdOrigin;
    @BindView(R2.id.ivTogglePwdOrigin)
    ImageView ivTogglePwdOrigin;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivTogglePwd)
    ImageView ivTogglePwd;
    @BindView(R2.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R2.id.ivTogglePwdAgain)
    ImageView ivTogglePwdAgain;
    @BindView(R2.id.btReset)
    Button btReset;
    private boolean isSeeAgain;
    private boolean isSee;
    private boolean isSeeOrigin;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private String account;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_pwd_change;
    }

    @Override
    public PwdChangeActivityPresenter getPresenter() {
        return new PwdChangeActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_activity_pwd_reset109));
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                account = TextUtils.isEmpty(userData.getMobile()) ? userData.getMail() : userData.getMobile();
            }
        });
        ClickUtil.click(btReset, () -> {
            if (TextUtils.isEmpty(etPwdOrigin.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minePwdChangeActivity0));
                return;
            }
            if (!CheckUtil.isPayPwd(etPwd.getText().toString().trim()) || !CheckUtil.isPayPwd(etPwdAgain.getText().toString().trim())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minePwdChangeActivity1));
                return;
            }
            if (!etPwd.getText().toString().trim().equals(etPwdAgain.getText().toString().trim())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minePwdChangeActivity2));
                return;
            }
            mPresenter.changePwd(etPwdOrigin.getText().toString(), etPwd.getText().toString(), etPwdAgain.getText().toString().trim());

        });
        ClickUtil.click(200, ivTogglePwdOrigin, () -> {
            if (isSeeOrigin) {
                isSeeOrigin = false;
                etPwdOrigin.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivTogglePwdOrigin.setImageResource(R.mipmap.mine_icon_unsee);
            } else {
                isSeeOrigin = true;
                etPwdOrigin.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivTogglePwdOrigin.setImageResource(R.mipmap.mine_icon_see);
            }
        });
        ClickUtil.click(200, ivTogglePwdAgain, () -> {
            if (isSeeAgain) {
                isSeeAgain = false;
                etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivTogglePwdAgain.setImageResource(R.mipmap.mine_icon_unsee);
            } else {
                isSeeAgain = true;
                etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivTogglePwdAgain.setImageResource(R.mipmap.mine_icon_see);
            }
        });
        ClickUtil.click(200, ivTogglePwd, () -> {
            if (isSee) {
                isSee = false;
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivTogglePwd.setImageResource(R.mipmap.mine_icon_unsee);
            } else {
                isSee = true;
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivTogglePwd.setImageResource(R.mipmap.mine_icon_see);
            }
        });
    }

    public void resetSuccess() {
        finish();
        LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
        logoutProvider.logout();
    }

}
